(function () {

    //编辑个人中心
    var account = document.getElementById('account');
    var userName = document.getElementById('user-name');
    var userSex = document.getElementsByClassName('user-sex');
    var userTel = document.getElementById('user-tel');
    var userEmail = document.getElementById('user-email');
    var userSign = document.getElementById('user-sign');
    var userAddress =document.getElementById('user-address');
    var inputUserInfo = document.getElementById('input_userInfo');


    //修改密码
    var userPassword = document.getElementById('password');
    var passwordTrue = document.getElementById('password_true');
    var inputPassword = document.getElementById('input_password');

    //用户信息
    var userInfo;

    initUserInfo();
    initListener();
    function initUserInfo() {
        userInfo = CommonUtil.isLogin();
        if (userInfo == null){
            mdui.alert('尚未登陆', function () {

            });
            return;
        }
        account.value = userInfo.account;
        userName.value = userInfo.name;
        for(var i = 0; i < userSex.length; i++){
            if (i == userInfo.sex){
                userSex.item(i).checked = true;
            }
        }
        userTel.value = userInfo.tel;
        userEmail.value = userInfo.email;
        userSign.value = userInfo.sign;
        userAddress.value = userInfo.address;
    }

    function initListener() {
        inputUserInfo.addEventListener('click', inputUserInfoListener);
        inputPassword.addEventListener('click', inputUserPasswordListener);
    }

    function inputUserPasswordListener() {
        if (userPassword.value == passwordTrue.value){
            userInfo.password = userPassword.value;
        }
        CommonUtil.setCookie('userInfo', JSON.stringify(userInfo));
        input(JSON.stringify(userInfo));
    }

    function inputUserInfoListener() {
        userInfo.name = userName.value;
        for(var i = 0; i < userSex.length; i++){
            if (userSex.item(i).checked == true){
                userInfo.sex = i;
            }
        }
        userInfo.tel = userTel.value;
        userInfo.email = userEmail.value;
        userInfo.sign = userSign.value;
        userInfo.address = userAddress.value;
        CommonUtil.setCookie('userInfo', JSON.stringify(userInfo));
        input(JSON.stringify(userInfo));
    }

    function input(userInfoJson) {
        Api.ajax({
            method: 'POST',
            url: 'updateUserInfo',
            data: {
                updateUserInfo: userInfoJson
            },
            success: function (response) {
                if (response.code == 0) {
                    console.log(response);
                    mdui.alert('更新成功');
                } else {
                    console.log(response.message);
                    mdui.alert(response.message);
                }
            }
        })
    }


})();